package com.getpebble.android.common.model;

import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ObjectUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppVersion implements Comparable<AppVersion> {
    private static final String TAG = AppVersion.class.getSimpleName();
    private final boolean mIsValidVersion;
    private final int mMajor;
    private final int mMinor;
    private final String mVersionTag;

    public AppVersion(String versionTag) {
        if (versionTag == null) {
            versionTag = "";
        }
        this.mVersionTag = versionTag;
        Matcher matcher = Pattern.compile("([\\d]+)\\.([\\d]+)(\\.\\d)?").matcher(this.mVersionTag);
        if (matcher.matches()) {
            this.mMajor = Integer.valueOf(matcher.group(1)).intValue();
            this.mMinor = Integer.valueOf(matcher.group(2)).intValue();
            this.mIsValidVersion = validateMajorMinor();
            return;
        }
        this.mIsValidVersion = false;
        this.mMajor = 0;
        this.mMinor = 0;
        Trace.info(TAG, "Version '" + this.mVersionTag + "' is not a valid app version string");
    }

    public AppVersion(int major, int minor) {
        this.mMajor = major;
        this.mMinor = minor;
        this.mVersionTag = major + "." + minor;
        this.mIsValidVersion = validateMajorMinor();
    }

    private boolean validateMajorMinor() {
        Trace.verbose(TAG, "validating (tag: " + this.mVersionTag + ") major: " + this.mMajor + " minor: " + this.mMinor);
        if (this.mMajor < 0 || this.mMajor > 255) {
            Trace.warning(TAG, "major version: " + this.mMajor + " is not valid");
            return false;
        } else if (this.mMinor >= 0 && this.mMinor <= 255) {
            return true;
        } else {
            Trace.warning(TAG, "minor version: " + this.mMinor + " is not valid");
            return false;
        }
    }

    public int getMajor() {
        return this.mMajor;
    }

    public int getMinor() {
        return this.mMinor;
    }

    public boolean isValidVersion() {
        return this.mIsValidVersion;
    }

    public String getVersionTag() {
        return this.mVersionTag;
    }

    public String toString() {
        return getVersionTag() + " (" + this.mMajor + " / " + this.mMinor + ") isValidVersion = " + isValidVersion();
    }

    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (o instanceof AppVersion) {
            return ObjectUtil.nullCheckEquals(((AppVersion) o).getVersionTag(), getVersionTag());
        }
        return false;
    }

    public int hashCode() {
        return this.mVersionTag != null ? this.mVersionTag.hashCode() : 0;
    }

    public int compareTo(AppVersion otherAppVersion) {
        if (otherAppVersion == null || !isValidVersion() || !otherAppVersion.isValidVersion()) {
            return 0;
        }
        if (getMajor() < otherAppVersion.getMajor()) {
            return -1;
        }
        if (getMajor() > otherAppVersion.getMajor()) {
            return 1;
        }
        if (getMinor() < otherAppVersion.getMinor()) {
            return -1;
        }
        if (getMinor() > otherAppVersion.getMinor()) {
            return 1;
        }
        return 0;
    }
}
